package com.svn.ui.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.DefaultListModel;

import com.svn.operate.SVNConnectMange;

public class UserListModel extends DefaultListModel {

    private List<String> allUsers;

    public UserListModel(String filePath) {
	this.addElement("jing.tang");
	this.addElement("jing.jing");
	String fileContent = "";
	try {
	    fileContent = SVNConnectMange.getDefaulSVNConnect().getFileByPath(
		    filePath);
	} catch (Exception e) {
	    e.printStackTrace();
	}
	initList(fileContent);
    }

    private void initList(String content) {

	allUsers = new ArrayList<String>();
	if ("".equals(content))
	    return;
	content = content.replace("\n", ":");
	String[] split = content.split(":");
	for (int i = 0; i < split.length; i=i+2) {
	    allUsers.add(split[i]);
	}
	Collections.sort(allUsers);
    }

    public void refresh(String user) {
	this.removeAllElements();
	String[] split = user.split(",");
	user = split[split.length-1];
	for (String username : allUsers) {
	    if(username.indexOf(user) != -1)
		this.addElement(username);
	}
    }
    public static void main(String[] args) {
	String t="tang,asdfasfd,ed";
	System.out.println(t.substring(0,t.lastIndexOf(",")));
    }
}
